*===============================================================================
*
*					WORKER BELIEFS ABOUT OUTSIDE OPTIONS
*		(c)	Simon Jaeger, Christopher Roth, Nina Roussille, Benjamin Schoefer
*							  2023 December 5
*						   	   SOEP-IAB Data 
*
*===============================================================================


********************************************************************************
*								Table A02	 								   *
********************************************************************************

cap log close        
log using ${log}/TableA02.log, replace    

* loading dataset of IAB individuals with cleaned characteristics (created by 5_machinelearning.do)
use "$data/machinelearning_chars.dta", clear 

* identifying movers by merging on a list of EUE movers (created by 5_machinelearning.do)
* note this dataset, by definition, doesn't contain SOEP individuals
merge 1:1 prs_id betnr jahr using "$data/machinelearning_unempFT.dta", keep(master match)

gen mover = _merge==3
drop _merge

replace ln_wage = ln_wage_gsoep if mover==0

* identifying main-sample SOEP individuals
cap drop gsoep
cap drop _merge
merge m:1 pid using "$data/unionsample_ids.dta", keep(master match)
gen gsoep = _merge==3
drop _merge

* EB sample 
cap drop gsoep_eb	
merge m:1 pid using "$data/unionsample_finaldata.dta", keepusing(ebayes_delta_ln_wage_mover_2015 ln_own_change)
gen gsoep_eb = _merge==3
drop _merge
gen emp_bayes = 0
replace emp_bayes = 1 if ebayes_delta_ln_wage_mover_2015!=. & ln_own_change !=.
replace gsoep = emp_bayes // restrict gsoep sample to EB sample 
egen maxgsoep = max(gsoep), by(betnr)
keep if maxgsoep==1
egen maxfirmmover = max(mover), by(betnr)
keep if maxfirmmover==1
drop maxfirmmover maxgsoep

* making dataset unique by person-firm, randomly
egen maxmover = max(mover), by(prs_id betnr)
keep if mover==maxmover
drop maxmover
gen randomvar = runiform()
egen maxrand = max(randomvar), by(prs_id betnr)
keep if randomvar==maxrand
drop maxrand randomvar

* generate occupation variables
gen occ1 = floor(occupation/10000)	
quietly egen occgroup = group(occ1)
quietly su occgroup
local occmax = r(max)
forvalues oc = 1/`occmax' {
	gen od`oc' = occgroup==`oc'
	su od`oc'
}
drop educ		

* collapsing within firm, by mover/nonmover/SOEP
gen collapse_cat = .
replace collapse_cat = 1 if gsoep==1
replace collapse_cat = 2 if mover==1
replace collapse_cat = 3 if gsoep==0 & mover==0

su collapse_cat 

local varlist "ln_wage age1 tenure1 female ed1 ed2 ed3 ed4 od1 od2 od3 od4 od5 od6 od7 od8 od9"

gen cat_mover = .
replace cat_mover = 1 if collapse_cat == 2
replace cat_mover = 0 if collapse_cat != 2

gen cat_nonmover = .
replace cat_nonmover = 1 if collapse_cat == 3	
replace cat_nonmover = 0 if collapse_cat != 3	

replace female = female - 1

table collapse_cat

	
* Comparison of means (collapsed on firm level)
* collapse on firm and category 
collapse (mean) `varlist', by(collapse_cat betnr)
sort betnr collapse_cat	

matrix J=J(17,5,.)
local j = 1 // counter for varlist in loop to set row number
foreach var in `varlist'{
	* means by group
	forvalues m=1/3{
		sum `var' if collapse_cat == `m'  
		mat J[`j',`m'] = r(mean)
	} // one col. per subsample mean (SOEP, mover, nonmover)
	* group comparison
	ttest `var' if collapse_cat == 1 | collapse_cat == 2, by(collapse_cat) // vs Movers 
	mat J[`j',4] = r(p)  
	ttest `var' if collapse_cat == 1 | collapse_cat == 3, by(collapse_cat) // vs Non-Movers 
	mat J[`j',5] = r(p)  
	local j = `j' + 1		
}

frmttable using "${log}/TableA02.tex", tex fragment statmat(J) replace ///
ctitle("Variable" , "Mean SOEP" ,"Mean Movers", "Mean Nonmovers", "SOEP vs. Movers", "SOEP vs. Nonmovers") ///
sdec(3) ///
rtitles("Log Wage" ///
\ "Age in Years" \ "Tenure in Years" \ "Share of Women" ///
\ "No Qualifications" ///
\ "Vocational Qualification" \ "University Qualification" \ "Missing Qualifications" ///
\ "Agriculture \& Forestry" ///
\ "Mining, Industry, \& Manufacturing" \ "Construction \& Infrastructure" \ ///
"Academic \& Technical" \ "Transportation, Logistics, \& Cleaning" \ "Sales" \ ///
"Managerial" \ "Medical, Childcare, \& Educational" \ "Marketing, Artistic, \& Athletic") 
	



cap log close
clear

